<template>
    <div>
        <iview-table
                ref="table"
                :span1="19"
                :span2="5"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeTotalSize:changeTotalSize,changeSelect:changeSelect,events:events,dblclick:dblclick}">
        </iview-table>
        <br>
        <iview-page
                style="margin-bottom: 200px"
                :totalSize="pageModel.totalSize"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :pageSizeOpts="pageModel.pageSizeOpts"
                v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
        </iview-page>
        <Modal v-model="showAdd" :closable='false' :mask-closable=true :width="1000">
            <h3 slot="header" style="color:#2D8CF0">请选择上传文件</h3>
            <vue-dropzone style="height: 500px" ref="dropzone" id="dropzone" :options="dropzoneOptions" v-on="{'vdropzone-file-added':added,'vdropzone-complete':complete,'vdropzone-sending':sending}"></vue-dropzone>
            <div slot="footer">
            </div>
        </Modal>
        <Modal v-model="showImage" :closable='false' :mask-closable=true width="100%">
            <img width="100%" height="100%" :src="path"/>
            <div slot="footer">
            </div>
        </Modal>
        <form id="downloadForm" :action="downloadUrl" method="post">
            <input type="hidden" id="id" name="id">
        </form>
        <iframe id="rfFrame" name="rfFrame" src="about:blank" style="display:none;"></iframe>
    </div>
</template>

<script>
    import vueDropzone from 'vue2-dropzone'
    import 'vue2-dropzone/dist/vue2Dropzone.min.css'
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    export default {
        components: {
            iviewTable,
            iviewPage,
            vueDropzone
        },
        props: {
            menuId: {
                type: [Number, String]
            },
            type: {
                type: [Number, String]
            }
        },
        data () {
            return {
                showAdd: false,
                showImage: false,
                dropzoneOptions: {
                    url: this.$store.state.app.UPLOADURL + '/file/upload',
                    thumbnailWidth: 150,
                    maxFiles: 10,
                    maxFilesize: 20,
                    // acceptedFiles: 'image/*',
                    dictDefaultMessage: '请点击该区域选择文件或把文件拖入该区域',
                    dictInvalidFileType: '不受支持的文件类型',
                    dictRemoveFile: '删除',
                    dictMaxFilesExceeded: '上传文件过多,一次最多可上传10个文件',
                    headers: { "My-Awesome-Header": "header value" },
                    addRemoveLinks: true
                },
                path: '',
                downloadUrl: this.$store.state.app.UPLOADURL + '/file/download',
                selectRows: [],
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/file',
                    defaultFilters: [
                        {'field': 'patient_id', 'value1': -1, 'operate': '=='},
                        {'field': 'complaint_id', 'value1': -1, 'operate': '=='}
                    ]
                },
                dataModel: {
                    menuId: this.menuId,
                    columns: [
                        {
                            type: 'selection',
                            width: 60,
                            align: 'center'
                        },
                        {
                            title: '上传时间',
                            key: 'creationTime',
                            render: (h, params) => {
                                return h('span', this.$moment(params.row.creationTime).format('YYYY年MM月DD日 HH时mm分'))
                            }
                        },
                        {
                            title: '文件名',
                            key: 'name'
                        },
                        {
                            title: '文件大小(KB)',
                            key: 'size',
                            render: (h, params) => {
                                return h('span', Math.floor(params.row.size / 1024))
                            }
                        },
                        {
                            title: '缩略图',
                            render: (h, params) => {
                                return h('a', {
                                    on: {
                                        click: () => {
                                            this.path = params.row.path
                                            this.showImage = true
                                        }
                                    }
                                }, [
                                    h('Avatar', {
                                        style: {

                                        },
                                        props: {
                                            src: params.row.path,
                                            shape: 'square',
                                            size: 'large'
                                        }
                                    })
                                ])
                            }
                        }
                    ],
                    order: 'id desc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [10, 50, 100, 500]
                }
            };
        },
        methods: {
            events (item) {
                let path = item.path
                if (path === 'upload') {
                    this.upload()
                } else if (path === 'download') {
                    this.download()
                } else if (path === 'delete') {
                    this.delete()
                }
            },
            dblclick (row, index) {
                document.getElementById('id').value = row.id
                document.getElementById('downloadForm').target = 'rfFrame';
                document.getElementById('downloadForm').submit()
            },
            added (file) {
            },
            sending (file, xhr, formData) {
                formData.append("type", this.type);
                formData.append("patientId", this.$store.state.app.patientId);
                formData.append("complaintId", this.$store.state.app.complaintId);
                formData.append("accountId", this.$store.state.app.loginUser.accountId);
                formData.append("organId", this.$store.state.app.loginUser.organId);
                formData.append("organName", this.$store.state.app.loginUser.organName);
            },
            complete (file) {
                this.refresh()
            },
            upload () {
                this.$refs.dropzone.removeAllFiles(true)
                this.showAdd = true
            },
            download () {
                if (this.selectRows.length === 1) {
                    document.getElementById('id').value = this.selectRows[0].id
                    document.getElementById('downloadForm').target = 'rfFrame';
                    document.getElementById('downloadForm').submit()
                } else {
                    this.$bus.alert('warning', '请选择一条记录')
                }
            },
            delete () {
                if (this.selectRows.length > 0) {
                    let ids = []
                    for (let row of this.selectRows) {
                        ids.push(row.id)
                    }
                    this.$Modal.confirm({
                        title: '操作提示',
                        content: '<p>确认删除?</p>',
                        onOk: () => {
                            this.doDelete(ids)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '请至少选择一条记录')
                }
            },
            doDelete (ids) {
                let url = '/file/delete'
                this.$file.post(url, ids).then(r => {
                    let {code, msg} = r.data
                    if (code === 0) {
                        this.$bus.alert('success', '删除成功')
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            refresh () {
                this.queryModel.defaultFilters.length = 0
                this.queryModel.defaultFilters.push({'field': 'type', 'value1': this.type, 'operate': '=='})
                if (this.$store.state.app.patientId) {
                    this.queryModel.defaultFilters.push({'field': 'patient_id', 'value1': this.$store.state.app.patientId, 'operate': '=='})
                }
                if (this.$store.state.app.complaintId) {
                    this.queryModel.defaultFilters.push({'field': 'complaint_id', 'value1': this.$store.state.app.complaintId, 'operate': '=='})
                }
                this.$refs.table.refresh()
            },
            changeSelect (rows) {
                this.selectRows = rows
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            },
            init () {
                this.refresh()
            }
        },
        mounted () {
            this.init()
        }
    }
</script>

<style>

</style>
